{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Create by 2018-05-21\n",
    "\n",
    "@author: Shiyipaisizuo\n",
    "\"\"\"\n",
    "import matplotlib.pyplot as plt\n",
    "from numpy import *\n",
    "import math\n",
    "from numpy.ma import array\n",
    "\n",
    "\n",
    "# 加载数据\n",
    "def load_data_set():\n",
    "    data_matrix = []\n",
    "    label_matrix = []\n",
    "\n",
    "    fr = open('testSet.txt')\n",
    "\n",
    "    for line in fr.readlines():\n",
    "        line_array = line.strip().split()\n",
    "        data_matrix.append([1.0, float(line_array[0]), float(line_array[1])])\n",
    "        label_matrix.append(int(line_array[2]))\n",
    "\n",
    "    return data_matrix, label_matrix\n",
    "\n",
    "\n",
    "def sigmoid(X):\n",
    "\n",
    "    return (1.0 / (1+math.exp(-X))) + 1e-8\n",
    "\n",
    "\n",
    "# Logistic回归梯度上升优化算法\n",
    "def grad_ascent(data_matrix, class_label):\n",
    "\n",
    "    # 将数据转化为numpy矩阵\n",
    "    data_matrix = mat(data_matrix)\n",
    "    label_matrix = mat(class_label).transpose()\n",
    "    m, n = shape(data_matrix)\n",
    "\n",
    "    alpha = 0.01\n",
    "    max_cycles = 500\n",
    "    weights = ones((n, 1))\n",
    "\n",
    "    # 矩阵之间做乘法\n",
    "    for k in range(max_cycles):\n",
    "        h = sigmoid(data_matrix*weights)\n",
    "        error = (label_matrix - h)\n",
    "        weights = weights + alpha * data_matrix.transpose() * error\n",
    "\n",
    "    return weights\n",
    "# 画图\n",
    "def plot_best_fit(weights):\n",
    "    data_matrix, label_matrix = load_data_set()\n",
    "    data_array = array(data_matrix)\n",
    "    n = shape(data_array)[0]\n",
    "    x1 = []\n",
    "    y1 = []\n",
    "    x2 = []\n",
    "    y2 = []\n",
    "\n",
    "    for i in range(n):\n",
    "        if int(label_matrix[i]) == 1:\n",
    "            x1.append(data_array[i, 1])\n",
    "            x2.append(data_array[i, 2])\n",
    "        else:\n",
    "            y1.append(data_array[i, 1])\n",
    "            y2.append(data_array[i, 2])\n",
    "\n",
    "    fig = plt.figure()\n",
    "    ax = fig.add_subplot(111)\n",
    "\n",
    "    ax.scatter(x1, x2, s=30, c='red', marker='s')\n",
    "    ax.scatter(y1, y2, s=30, c='green')\n",
    "    x = arange(-3.0, 3.0, 0.1)\n",
    "\n",
    "    # 最佳似合直线\n",
    "    y = (-weights[0] - weights[1]*x) / weights[2]\n",
    "    ax.plot(x, y)\n",
    "    plt.xlabel('X1')\n",
    "    plt.ylabel('X2')\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "# 随机梯度上升算法\n",
    "def stroc_grad_ascent0(data_matrix, class_labels):\n",
    "    m, n = shape(data_matrix)\n",
    "    alpha = 0.01\n",
    "    weights = ones(n)\n",
    "\n",
    "    for i in range(m):\n",
    "        h = sigmoid(sum(data_matrix[i] * weights))\n",
    "        error = class_labels[i] - h\n",
    "        weights = weights + alpha * error * data_matrix[i]\n",
    "\n",
    "    return weights\n",
    "\n",
    "\n",
    "# 改进后的随机梯度上升算法\n",
    "def stoc_grad_ascent1(data_matrix, class_labels, num_iter=150):\n",
    "    m, n = shape(data_matrix)\n",
    "    weights = ones(n)\n",
    "\n",
    "    for j in range(num_iter):\n",
    "        data_index = list(range(m))\n",
    "        for i in range(m):\n",
    "\n",
    "            # 每次来调整alpha的值\n",
    "            alpha = 4 / (1.0 + j + i) + 0.0001\n",
    "\n",
    "            # 随机选取更新\n",
    "            rand_index = int(random.uniform(0, len(data_index)))\n",
    "            h = sigmoid(sum(data_matrix[rand_index] * weights))\n",
    "            error = class_labels[rand_index] - h\n",
    "            weights = weights + alpha * error * data_matrix[rand_index]\n",
    "            del (data_index[rand_index])\n",
    "\n",
    "    return weights\n",
    "\n",
    "\n",
    "# Logistic回归分类函数\n",
    "def classify_vector(inx, weights):\n",
    "    prob = sigmoid(sum(inx * weights))\n",
    "\n",
    "    if prob > 0.5:\n",
    "        return 1.0\n",
    "    else:\n",
    "        return 0.0\n",
    "\n",
    "\n",
    "def colic_test():\n",
    "    frtrain = open('horseColicTraining.txt');\n",
    "    frtest = open('horseColicTest.txt')\n",
    "    train_set = []\n",
    "    train_labels = []\n",
    "\n",
    "    for line in frtrain.readlines():\n",
    "        curr_line = line.strip().split('\\t')\n",
    "        line_array = []\n",
    "        for i in range(21):\n",
    "            line_array.append(float(curr_line[i]))\n",
    "        train_set.append(line_array)\n",
    "        train_labels.append(float(curr_line[21]))\n",
    "\n",
    "    train_weights = stoc_grad_ascent1(array(train_set), train_labels, 1000)\n",
    "    error_count = 0\n",
    "    num_test_vector = 0.0\n",
    "\n",
    "    for line in frtest.readlines():\n",
    "        num_test_vector += 1.0\n",
    "        curr_line = line.strip().split('\\t')\n",
    "        line_array = []\n",
    "        for i in range(21):\n",
    "            line_array.append(float(curr_line[i]))\n",
    "        if int(classify_vector(array(line_array), train_weights)) != int(curr_line[21]):\n",
    "            error_count += 1\n",
    "\n",
    "    error_rate = (float(error_count) / num_test_vector)\n",
    "    print(f\"the error rate of this test is: {error_rate}\")\n",
    "\n",
    "    return error_rate\n",
    "\n",
    "\n",
    "# 多重测试\n",
    "def mul_test():\n",
    "    num_tests = 10\n",
    "    error_sum = 0.0\n",
    "\n",
    "    for k in range(num_tests):\n",
    "        error_sum += colic_test()\n",
    "\n",
    "    print(f\"after {num_tests} iterations the average error rate is: {error_sum / float(num_tests)}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from numpy import *\n",
    "import math\n",
    "from numpy.ma import array\n",
    "\n",
    "\n",
    "# 加载数据\n",
    "def load_data_set():\n",
    "    data_matrix = []\n",
    "    label_matrix = []\n",
    "\n",
    "    fr = open('testSet.txt')\n",
    "\n",
    "    for line in fr.readlines():\n",
    "        line_array = line.strip().split()\n",
    "        data_matrix.append([1.0, float(line_array[0]), float(line_array[1])])\n",
    "        label_matrix.append(int(line_array[2]))\n",
    "\n",
    "    return data_matrix, label_matrix\n",
    "def sigmoid(X):\n",
    "    return 1.0 / (1+exp(-X))\n",
    "\n",
    "# Logistic回归梯度上升优化算法\n",
    "def grad_ascent(data_matrix, class_label):\n",
    "\n",
    "    # 将数据转化为numpy矩阵\n",
    "    data_matrix = mat(data_matrix)\n",
    "    label_matrix = mat(class_label).transpose()\n",
    "    #m,n=100,3\n",
    "    \n",
    "    m, n = shape(data_matrix)\n",
    "    print(m,'`````',n)\n",
    "    alpha = 0.01\n",
    "    max_cycles = 50\n",
    "    weights = ones((n, 1))\n",
    "\n",
    "    # 矩阵之间做乘法\n",
    "    for k in range(max_cycles):\n",
    "        #h是100*1的矩阵\n",
    "        h = sigmoid(data_matrix*weights)\n",
    "        \n",
    "        error = (label_matrix - h)\n",
    "        weights = weights + alpha * data_matrix.transpose() * error\n",
    "#         print(weights)\n",
    "    return weights\n",
    "\n",
    "dataArr,labelMat=load_data_set()\n",
    "\n",
    "# grad_ascent(dataArr, labelMat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100 ````` 3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "matrix([[ 7.58838963],\n",
       "        [ 1.68267558],\n",
       "        [-0.76052004]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weights=grad_ascent(dataArr, labelMat)\n",
    "weights\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAEGCAYAAAB7DNKzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0d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wxxgRlQWGMMSYoCwpjjDFBWVAYY4wJyoLCGGNMUBYUxhhjgvr/W8FRgvhbg48AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画图\n",
    "def plot_best_fit(weights):\n",
    "    data_matrix, label_matrix = load_data_set()\n",
    "    data_array = array(data_matrix)\n",
    "#     print(data_array)\n",
    "    #n:100\n",
    "    n = shape(data_array)[0]\n",
    "    print(n)\n",
    "    x1 = []\n",
    "    y1 = []\n",
    "    x2 = []\n",
    "    y2 = []\n",
    "\n",
    "    for i in range(n):\n",
    "        if int(label_matrix[i]) == 1:\n",
    "            x1.append(data_array[i][1])\n",
    "#             print(x1)\n",
    "            x2.append(data_array[i][2])\n",
    "        else:\n",
    "            y1.append(data_array[i][1])\n",
    "            y2.append(data_array[i][2])\n",
    "\n",
    "    fig = plt.figure()\n",
    "    ax = fig.add_subplot(111)\n",
    "\n",
    "    ax.scatter(x1, x2, s=30, c='red', marker='s')\n",
    "    ax.scatter(y1, y2, s=30, c='green')\n",
    "    x = arange(-3.0, 3.0, 0.1)\n",
    "\n",
    "    # 最佳似合直线\n",
    "    y = (-weights[0] - weights[1]*x) / weights[2]\n",
    "    ax.plot(x, y)\n",
    "    plt.xlabel('X1')\n",
    "    plt.ylabel('X2')\n",
    "    plt.show()\n",
    "plot_best_fit(weights.getA())\n",
    "\n",
    "#将分类给画出来，画出最佳的拟合直线  \n",
    "def plotBestFit(weights):  \n",
    "    \"\"\" \n",
    "    :param weights: 最佳回归系数数组 \n",
    "    :return: \n",
    "    \"\"\"  \n",
    "    dataMat, labelMat = loadDataSet()       #加载数据集，返回数据集合分类  \n",
    "    dataArr = array(dataMat)        #将数据集数组化  \n",
    "    dataShape = shape(dataArr)      #data的形状  \n",
    "    print(\"data的形状：\", dataShape)  \n",
    "    n = dataShape[0]        #形状的第一个是行数（即数据集的个数），第二个是列数（即数据集的特征）  \n",
    "    xcord1 = []     #分类为1的点  \n",
    "    ycord1 = []  \n",
    "    xcord0 = []     #分类为0的点  \n",
    "    ycord0 = []  \n",
    "    for i in range(n):  \n",
    "        if int(labelMat[i]) == 1:       #如果分类为1，添加到1分类的点集，否者返回到0分类的点集  \n",
    "            xcord1.append(dataArr[i, 1])        #这个dataArr有3列，其中第0列为1，第1,2列为x，y  \n",
    "            ycord1.append(dataArr[i, 2])  \n",
    "        else:  \n",
    "            xcord0.append(dataArr[i, 1])  \n",
    "            ycord0.append(dataArr[i, 2])  \n",
    "    fig = plt.figure()      #figure()函数创建一个新的图  \n",
    "    ax = fig.add_subplot(111)       #add_subplot()函数在一张figure里面生成多张子图参数111，表示1行1列第1个位置  \n",
    "    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')       #散点图  \n",
    "    ax.scatter(xcord0, ycord0, s=30, c='green')  \n",
    "    # 直线 x 的范围  \n",
    "    x = arange(-3.0, 3.0, 0.1)      # (start, end, step)可以调step执行起来，看看图  \n",
    "    # 画出直线，weights[0]*1.0+weights[1]*x+weights[2]*y=0  \n",
    "    # 之前计算时对原始数据做了拓展，将两维拓展为三维，第一维全部设置为1.0，实际他是一个 y=ax+b, b常量  \n",
    "    y = (-weights[0]-weights[1]*x)/weights[2]  \n",
    "    ax.plot(x, y)       #画出直线  \n",
    "    plt.xlabel('x1')        #X轴标记为x1  \n",
    "    plt.ylabel('x0')        #Y轴标记为x0  \n",
    "    plt.show()  \n",
    "  \n",
    "plotBestFit(weights.getA())  #getA()将weights矩阵转换为数组，getA()函数与mat()函数的功能相反  \n",
    "\"\"\" \n",
    "如果是矩阵的话会报这样的错： \n",
    "\"have shapes {} and {}\".format(x.shape, y.shape)) \n",
    "ValueError: x and y must have same first dimension, but have shapes (60,) and (1, 60) \n",
    "为啥要用数组呢？因为 x = arange(-3.0, 3.0, 0.1)，len(x) = [3-(-3)]/0.1 = 60 \n",
    "而weights是矩阵的话，y = (-weights[0]-weights[1]*x)/weights[2]，len(y) = 1，有60个x，y只有一个，你这样都画不了线 \n",
    "而weights是数据的话，len(y) = 60 \n",
    "\"\"\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAEGCAYAAAB7DNKzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3deXxU9dX48c+BsMi+byGRPYgKCGnQUhQFAal1r8W6tbYiVlFbEbv8+rRP+3t+TwliXVBZXNvi1rrUWmRTEUF2BEEhENaEsIUlJASynt8fGWgYZibbzNx7Z8779cormTt3Zk4yk3vu/W5HVBVjjDEmmHpOB2CMMcbdLFEYY4wJyRKFMcaYkCxRGGOMCckShTHGmJASnA4gEtq1a6fdunVzOgxjjPGMtWvX5qpq+0D3xWSi6NatG2vWrHE6DGOM8QwR2R3sPmt6MsYYE5IlCmOMMSFZojDGGBOSJQpjjDEhRTxRiMjLInJQRDZV2vZ7EdkrIut9X2ODPHaMiGSISKaI/DLSsRpjjDlXNK4oXgXGBNj+Z1Ud6Pua63+niNQHngOuAfoBt4lIv4hGaowx5hwRTxSqugQ4UouHpgGZqrpDVYuBN4HrwxqcMcaYKjnZR/GgiHzla5pqHeD+RCCr0u1s37aARGS8iKwRkTWHDh0Kd6zGeE5WXhYT504kbXYaE+dOJCsvq+oHGROAU4niBaAnMBDYB0wLsI8E2Ba0eIaqzlLVVFVNbd8+4ORCY+JGVl4WA2YMYObamazOWc3MtTMZMGOAJQtTK44kClU9oKplqloOzKaimclfNpBU6XZXICca8RnjdenL0ikoLqCkvASAkvISCooLSF+W7nBkxoscSRQi0rnSzRu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D/+V0PLVwD/CR00HEmBqtomzCQ0S6AZcAK52NpHpEpL6IrAcOAgtVNaxxW6KIoipW1EVVf6OqScAc4EFno/2PquL27fMboJSK2B1XnZg9okarKJu6E5FmwDvAI35X+q6lqmWqOpCKK/o0EQlrc5+TFe7iTnVX1AVeB/4N/C6C4VRbVXGLyN3AtcAIdUmnVw3+1m5nqyhHka+N/x1gjqq+63Q8NaWqx0RkMTCGwIut1opdUbiEiPSudPM6YItTsdSEiIwBHgeuU9VCp+OJQWdWURaRhlSsovyBwzHFJF+n8EvAZlV90ul4qktE2p8ebSgi5wEjCfPxw0Y9uYSIvAOkUDEaZzcwQVX3OhtV1UQkE2gEHPZtWuH20VoiciPwLNAeOAasV9XRzkYVnIiMBZ7iP6so/4/DIVVJRN4AhlOx9PUB4Heq+pKjQVVBRL4DfA5spOL/EODXvsVJXUtE+gOvUfH5qAe8rap/COtrWKIwxhgTijU9GWOMCckShTHGmJAsURhjjAnJEoUxxpiQLFEYY4wJyRKFMRHgW4l0p4i08d1u7bt9vojME5FjIvKh03EaUx2WKIyJAFXNAl4A/uTb9CdglqruBqYCdzoVmzE1ZYnCmMj5M3CpiDwCfAeYBqCqHwP5TgZmTE3YWk/GRIiqlojIY8A8YJRvmXBjPMeuKIyJrGuAfYAnivcYE4glCmMiREQGUlGZ7lLg574KasZ4jiUKYyLAtxLpC1TUNNhDRQf2E85GZUztWKIwJjLuBfao6kLf7eeBviJyhYh8DvwdGCEi2SLi2pVrjQFbPdYYY0wV7IrCGGNMSJYojDHGhGSJwhhjTEiWKIwxxoRkicIYY0xIliiMMcaEZInCGGNMSP8fD2w28wOpP24AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.01473994  0.85981505 -0.36563381]\n"
     ]
    }
   ],
   "source": [
    "def stroc_grad_ascent0(data_matrix, class_labels):\n",
    "    m, n = shape(data_matrix)\n",
    "    alpha = 0.01\n",
    "    weights = ones(n)\n",
    "    \n",
    "    for i in range(m):\n",
    "        h = sigmoid(sum(data_matrix[i] * weights))\n",
    "        error = class_labels[i] - h\n",
    "        weights = weights + alpha * error * data_matrix[i]\n",
    "    return weights\n",
    "weights=stroc_grad_ascent0(array(dataArr), labelMat)\n",
    "plot_best_fit(weights)\n",
    "print(weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAEGCAYAAAB7DNKzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO3df5TddX3n8edrfiWZyQxJICCEILQiiEpQshNdqsWiFKiK7toe2KXlVNckrkbtbjf+OmpXT/fY2NbuQoHAykprinb9gawEBbUelYMhCSYECyilyiRBCOb3L+bXe/+YO5M7d+79zq977/f7vff1OGdO7v3e79y870zyeX9+fxQRmJmZVdKSdgBmZpZtThRmZpbIicLMzBI5UZiZWSInCjMzS9SWdgC1cMopp8TZZ5+ddhhmZrmxdevW5yNicbnXGjJRnH322WzZsiXtMMzMckPSLyu95q4nMzNL5ERhZmaJnCjMzCyRE4WZmSWqeaKQdLuk5yQ9WnTtzyTtkrSt8HVVhe+9QtITkp6U9OFax2pmZhPVo0XxBeCKMtc/FxEXFb42lr4oqRX4W+BK4ALgWkkX1DRSMzOboOaJIiJ+AOydwbf2Ak9GxFMR0Q98Cbi6qsGZmdmk0hyjeJ+kRwpdUwvLvL4E6Ct6vrNwrSxJKyVtkbRlz5491Y7VLHf6DvSxZuMaem/rZc3GNfQd6Jv8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+cLC8xdL+pak/ZK+mXacZlPhRGFWAxHRB9wMfKZw6TPArRHxS+CzwB+mFZvZdDlRmNXO54DXSPog8FvAXwFExHeBQ2kGZjYd3uvJrEYiYkDSfwO+BVxe2CbcLHfcojCrrSuBZ4BcHN5jVo4ThVmNSLqIkZPpXgP8SeEENbPccaIwq4HCTqQ3M3KmwdOMDGD/ZbpRmc2ME4VZbbwbeDoi7i88vwk4X9JvS/oh8H+ByyTtlJTZnWvNwLvHmpnZJNyiMDOzRE4UZmaWyInCzMwSOVGYmVkiJwozM0vkRGFmZomcKMzMLNH/B2C5dz2rDN47AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 改进后的随机梯度上升算法\n",
    "def stoc_grad_ascent1(data_matrix, class_labels, num_iter=150):\n",
    "    m, n = shape(data_matrix)\n",
    "    weights = ones(n)\n",
    "    for j in range(num_iter):\n",
    "        data_index = list(range(m))\n",
    "        for i in range(m):\n",
    "\n",
    "            # 每次来调整alpha的值\n",
    "            alpha = 4 / (1.0 + j + i) + 0.0001\n",
    "\n",
    "            # 随机选取更新\n",
    "            rand_index = int(random.uniform(0, len(data_index)))\n",
    "            h = sigmoid(sum(data_matrix[rand_index] * weights))\n",
    "            error = class_labels[rand_index] - h\n",
    "            weights = weights + alpha * error * data_matrix[rand_index]\n",
    "            del (data_index[rand_index])\n",
    "    return weights\n",
    "weights=stoc_grad_ascent1(array(dataArr), labelMat,500)\n",
    "plot_best_fit(weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#预测病马的死亡率\n",
    "# Logistic回归分类函数\n",
    "def classify_vector(inx, weights):\n",
    "    prob = sigmoid(sum(inx * weights))\n",
    "    if prob > 0.5:\n",
    "        return 1.0\n",
    "    else:\n",
    "        return 0.0\n",
    "def colic_test():\n",
    "    frtrain = open('horseColicTraining.txt');\n",
    "    frtest = open('horseColicTest.txt')\n",
    "    train_set = []\n",
    "    train_labels = []\n",
    "    \n",
    "    for line in frtrain.readlines():\n",
    "        curr_line = line.strip().split('\\t')\n",
    "        line_array = []\n",
    "        for i in range(21):\n",
    "            line_array.append(float(curr_line[i]))\n",
    "        train_set.append(line_array)\n",
    "        train_labels.append(float(curr_line[21]))\n",
    "    train_weights = stoc_grad_ascent1(array(train_set), train_labels, 1000)\n",
    "    error_count = 0\n",
    "    num_test_vector = 0.0\n",
    "    for line in frtest.readlines():\n",
    "        num_test_vector += 1.0\n",
    "        curr_line = line.strip().split('\\t')\n",
    "        line_array = []\n",
    "        for i in range(21):\n",
    "            line_array.append(float(curr_line[i]))\n",
    "        if int(classify_vector(array(line_array), train_weights)) != int(curr_line[21]):\n",
    "            error_count += 1\n",
    "    error_rate = (float(error_count) / num_test_vector)\n",
    "    print(f\"the error rate of this test is: {error_rate}\")\n",
    "    return error_rate\n",
    "# 多重测试\n",
    "def mul_test():\n",
    "    num_tests = 10\n",
    "    error_sum = 0.0\n",
    "    for k in range(num_tests):\n",
    "        error_sum += colic_test()\n",
    "    print(f\"after {num_tests} iterations the average error rate is: {error_sum / float(num_tests)}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the error rate of this test is: 0.373134328358209\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.373134328358209"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colic_test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mul_test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.16.5\n",
      "0.25.1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from matplotlib.pyplot import plot\n",
    "print(np.__version__)\n",
    "print(pd.__version__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
